class Solution
{
public:
    long long numberOfWays(string s)
    {
        int n = s.size();
        vector<int> leftOneCount(n);
        vector<int> rightOneCount(n);
        vector<int> leftZeroCount(n);
        vector<int> rightZeroCount(n);

        leftOneCount[0] = 0;
        leftZeroCount[0] = 0;
        for (int i = 1; i < n; ++i)
        {
            if (s[i - 1] == '0')
            {
                leftZeroCount[i] = leftZeroCount[i - 1] + 1;
                leftOneCount[i] = leftOneCount[i - 1];
            }
            else
            {
                leftZeroCount[i] = leftZeroCount[i - 1];
                leftOneCount[i] = leftOneCount[i - 1] + 1;
            }
        }

        rightZeroCount[n - 1] = 0;
        rightOneCount[n - 1] = 0;
        for (int i = n - 2; i >= 0; --i)
        {
            if (s[i + 1] == '0')
            {
                rightZeroCount[i] = rightZeroCount[i + 1] + 1;
                rightOneCount[i] = rightOneCount[i + 1];
            }
            else
            {
                rightZeroCount[i] = rightZeroCount[i + 1];
                rightOneCount[i] = rightOneCount[i + 1] + 1;
            }
        }

        long long result = 0;
        for (int i = 0; i < n; ++i)
        {
            if (s[i] == '0')
            {
                result += static_cast<long long>(leftOneCount[i]) * rightOneCount[i];
            }
            else
            {
                result += static_cast<long long>(leftZeroCount[i]) * rightZeroCount[i];
            }
        }

        return result;
    }
};